package com.bidianying.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.stereotype.Service;

@Service
public class WebService {
	
	
	
	
	//格瓦拉获取影片信息
	public Map<String,Object> getFilmInfoByUrl(String url,Map<String,String> data,String citycode){
		int filmNum = 0;
		Map<String,Object> resultMap = new HashMap<String,Object>();
		List<Map<String,String>> list = new ArrayList<Map<String,String>>();
		for(int i = 0;i<=filmNum/10;i++){
			try {
				data.put("pageNo",i+"");
				Document doc = Jsoup.connect(url).data(data).cookie("citycode", citycode).get();
				Element filmNumEle = doc.getElementById("keyNum_mpiMovieCount");
				//影片总数
				filmNum = Integer.parseInt(filmNumEle.text());
				resultMap.put("filmNum", filmNum);
				//解析影片
				Elements effectLi = doc.getElementsByClass("effectLi");
				for(Element ele : effectLi){
					Map<String,String> map = new HashMap<String,String>();
					//获取影片 图片
					Element picLinks = ele.select("img").first();
					map.put("picLinks", picLinks.attr("src").replace("cw120h160", "cw150h200"));
					//获取影片类型。2d 还是 3d
					Element movieType = ele.select(".ui_movieType_line").first();
					if(movieType!=null){
						map.put("movieType", movieType.attr("title"));
					}else{
						map.put("movieType", "2d");
					}
					//影片信息
					Element title = ele.select("h2").first();
					if(title.text().length()>7){
						map.put("title", title.text().substring(0,7)+"...");
					}else{
						map.put("title", title.text());
					}
					String href = title.select("a").first().attr("href");
					map.put("href", href.substring(1));
					Element uText = ele.select(".ui_text").first();
					Elements pText = uText.select("p");
					String pSummary = "";
					for(Element p:pText){
						pSummary = pSummary + p.text() + "|";
					}
					pSummary = pSummary.substring(0,pSummary.length()-1);
					map.put("pSummary", pSummary);
					Element grade = ele.select(".grade").first();
					map.put("grade", grade.text().replace(" ", ""));
					list.add(map);
				}
				resultMap.put("data", list);
				
			} catch (IOException e) {
				e.printStackTrace();
				return new HashMap<String,Object>();
			}
		}
		return resultMap;
	}
	
	public static void main(String[] args) {
		Map<String,String> data = new HashMap<String,String>();
		data.put("playDate", "2015-04-29");
		data.put("movieid", "228845987");
		try {
			Document doc = Jsoup.connect("http://www.gewara.com/movie/v5/getCurAllCinemaList.xhtml").data(data).cookie("citycode", "440300").get();
			Element countyAreaInner = doc.getElementById("countyAreaInner");
			Elements area = countyAreaInner.select("a");
			Map<String,String> areaMap = new HashMap<String,String>();
			Map<String,String> areaCinemaMap = new HashMap<String,String>();
			for(Element e:area){
				String areaId = e.attr("id");
				String areaValue = e.text();
				areaMap.put(areaId, areaValue);
			}
			Element chooseCinema = doc.getElementById("wrapped_splayList");
			Elements cinemas = chooseCinema.select("li");
			for(Element element : cinemas){
				Map<String,String> map = new HashMap<String,String>();
				String areaId = element.attr("rel");
				String cinemaId = element.attr("cid");
				String cinemaName = element.select(".ui_edtType").first().text();
				String address = element.select(".address").first().text();
				map.put("areaId",areaId);
				map.put("cinemaId",cinemaId);
				map.put("cinemaName",cinemaName);
				map.put("address",address);
			}
			
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
